using System;
using System.Collections.Generic;
using System.Linq;

namespace TemplateWeb.Models.Repositories
{
	#region using
    using TemplateWeb.Models;
    using TemplateWeb.Helpers;
	#endregion
	public class GioHangRepository
	{
        public List<GioHang> SelectByGioHangID(string GioHangid)
        {
            try
            {
                return DataContext.Instance.GioHang.Where(item => item.CartId == GioHangid).ToList();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    CommonLogging.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    CommonLogging.Logger.Error(ex.Message);
                }
            }
            return null;
        }

        public GioHang SelectByRecordID(int recordid)
        {
            try
            {
                return DataContext.Instance.GioHang.FirstOrDefault(item => item.ID == recordid);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    CommonLogging.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    CommonLogging.Logger.Error(ex.Message);
                }
            }
            return null;
        }

        public GioHang SelectByIDAndRecordID(string GioHangid, int recordid)
        {
            try
            {
                return DataContext.Instance.GioHang.FirstOrDefault(item => item.CartId == GioHangid && item.ID == recordid);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    CommonLogging.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    CommonLogging.Logger.Error(ex.Message);
                }
            }
            return null;
        }

		public GioHang SelectByIDAndSanPham(string GioHangid, int idsanpham)
		{
			try
			{
                return DataContext.Instance.GioHang.FirstOrDefault(item => item.CartId == GioHangid && item.IDSanPham == idsanpham);
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public bool Insert(GioHang obj)
		{
			try
			{
				DataContext.Instance.GioHang.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public bool Delete(string GioHangid, int recordid)
		{
			try
			{
                var delitem = DataContext.Instance.GioHang.FirstOrDefault(item => item.CartId == GioHangid && item.ID == recordid);
				DataContext.Instance.GioHang.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			return false;
		}

        public bool Empty(string GioHangid)
        {
            try
            {
                var lstDel = DataContext.Instance.GioHang.Where(item => item.CartId == GioHangid).ToList();
                foreach (var item in lstDel)
                {
                    DataContext.Instance.GioHang.DeleteObject(item);
                }
                DataContext.Instance.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    CommonLogging.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    CommonLogging.Logger.Error(ex.Message);
                }
            }
            return false;
        }

		public bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			return false;
		}
	}
}